<template>
  <div>
    <div style="width: 99%;text-align: right;padding: 10px">
      <el-button type="default" @click="exportsImg">导出</el-button>
    </div>
    <div class="box">
      <div style="width: 100%;height: 300px" id="main"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  data(){
    return{

    }
  },
  mounted() {
    this.getDetails()
  },
  methods: {
    getDetails(){
      let chartDom = document.getElementById('main');
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'line'
          }
        ]
      };

      option && myChart.setOption(option);
    },
    exportsImg(){
      let chartDom = document.getElementById('main');
      let myChart = echarts.init(chartDom);

      let picInfo = myChart.getDataURL({
        type: 'png',
        pixelRatio: 1.5, //放大两倍下载，之后压缩到同等大小展示。解决生成图片在移动端模糊问题
        backgroundColor: '#fff',
      })
      console.log(picInfo)
      let newWindow = window.open();
      newWindow.document.write('<img src="' + picInfo + '">');
    },
  }
}
</script>